﻿import 'package:get/get.dart';
import 'package:azring/core/repository/default_api.dart';
import 'package:getx_scaffold/common/utils/toast_util.dart';

class NotificationLogic extends GetxController {
  final messageNotification = false.obs;
  final batteryNotification = false.obs;
  final heartRateAlert = false.obs;
  final planReminder = false.obs;

  final isLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    loadReminderSettings();
  }

  /// 加载提醒设置
  Future<void> loadReminderSettings() async {
    try {
      isLoading.value = true;
      final response = await DefaultApi().queryReminderSetGet(
        isShowLoading: false,
      );

      if (response != null && response['code'] == 200) {
        final data = response['data'];
        if (data != null) {
          // 将字符串 "1" 或 "0" 转换为布尔值
          messageNotification.value = data['informationReminder'] == '1';
          batteryNotification.value = data['lowBatteryAlert'] == '1';
          heartRateAlert.value = data['heartRateWarn'] == '1';
          planReminder.value = data['planReminder'] == '1';
        }
      }
    } catch (e) {
      print('加载提醒设置失败: $e');
    } finally {
      isLoading.value = false;
    }
  }

  /// 更新提醒设置
  Future<void> updateReminderSettings() async {
    try {
      // 将布尔值转换为字符串 "1" 或 "0"
      final response = await DefaultApi().operationReminderSetPost(
        informationReminder: messageNotification.value ? '1' : '0',
        lowBatteryAlert: batteryNotification.value ? '1' : '0',
        heartRateWarn: heartRateAlert.value ? '1' : '0',
        planReminder: planReminder.value ? '1' : '0',
        isShowLoading: false,
      );

      if (response != null && response['code'] == 200) {
        Toast.show( '提醒设置已更新');
      }
    } catch (e) {
      print('更新提醒设置失败: $e');
    }
  }
}
